<?php
	include_once("./global.php");
	session_start();
	if($_SESSION[id]){
		$url = $_SERVER['REQUEST_URI'];
		$url_arr  = parse_url($url);
		parse_str($url_arr['query'],$url_arr);
		$url_trackid = $url_arr[aid];
		
		if(isset($_POST['update_app'])){
			if($_POST['app_overhead'] == '0'){
				$update = '0';
				$pushpid = '0';
			}else{
				$update = $_POST['app_overhead'];
				if(is_uploaded_file($_FILES['uploaded']['tmp_name'])){
					$uploaded = $_FILES["uploaded"];
					if($uploaded['error'] > 0){
						echo "<script>alert('上传出错，请重试')</script>";
					}else{
						if($uploaded["size"] <= '3145728'){
							$date = mktime();	
							$name = $_POST['uploaded_name'];
							$type = $_FILES['uploaded']['type'];
							$image_name = $name.$date;
							$path = "../images/";
							$file_pathname = "../images/".$name.".png";				
							if(move_uploaded_file($_FILES['uploaded']['tmp_name'],$file_pathname)){
								$sql_insert_pic = "INSERT INTO pictureinfo(`P_PID`, `P_Name`, `P_ID`, `P_Path`, `P_Class`, `P_Adid`, `P_Adclass`, `P_Date`, `P_Url`) VALUES (NULL,'$name','$url_trackid','$path','2','1','$update','$date',NULL)";
								$query_insert_pic = $mysqli->query($sql_insert_pic);
								if($query_insert_pic){
									$sql_select_pid = "select P_PID from pictureinfo where P_Date = '$date'";
									$query_select_pid = $mysqli->query($sql_select_pid);
									$result_select_pid = $query_select_pid->fetch_assoc();
									if($result_select_pid){
										$A_Apppadid = $result_select_pid['P_PID'];
									}else{
										echo "<script>alert('图片查询失败')</script>";
									}
								}else{
									echo "<script>alert('图片添加失败')</script>";
									echo $mysqli->error.$url_trackid;
								}
							}else{
								echo "失败";
							}				
						}else{
							echo "<script>alert('图片太大')</script>";
						}
					}
				}else{
					echo "<script>alert('请选择你要上传的图片')</script>";
				}
				$sql_update_push = "update pushinfo set P_Pushartid = NULL, P_Pushappid='$url_trackid',P_Pushpid = '$A_Apppadid' where P_Pushid = '$update' ";
				$query_update_push = $mysqli->query($sql_update_push);
				if($query_update_push){
					echo "<script>alert('push更新成功')</script>";
					$sql_select_push = "select P_Pushid,P_Pushpid from pushinfo where P_Pushappid = '$url_trackid'";
					$query_select_push = $mysqli->query($sql_select_push);
					$result_select_push = $query_select_push->fetch_assoc();
					if($result_select_push){
						$pushid = $result_select_push['P_Pushid'];
						$pushpid = $result_select_push['P_Pushpid'];
						echo "<script>alert('push查询成功')</script>";
					}else{
						echo "<script>alert('pushid查询失败')</script>";
						echo $mysqli->error();
					}
				}else{
					echo "<script>alert('push更新失败')</script>";
					echo $mysqli->error();
				}
			}

			$date = mktime();
			$sql = "UPDATE `appinfo` SET `A_Class`='$_POST[update_class]',`A_Categoryid`='$_POST[update_category]',`A_Apptrackname`='$_POST[update_apptrackname]',`A_Appdesc`='$_POST[update_desc]',`A_Appkid`='$_POST[update_keyword]',`A_Appsystem`='$_POST[update_appsystem]',`A_Appartistname`='$_POST[update_appartistname]',`A_Apppid`='$_POST[update_appscreenshot]',`A_Appversion`='$_POST[update_appversion]',`A_Appicon`='$_POST[update_appicon]',`A_Appprice`='$_POST[update_price]',`A_Appfilesize`='$_POST[update_appfilesizebytes]',`A_Appreleasedate`='$_POST[update_appreleasedate]',`A_Apprating`='$_POST[update_apprating]',`A_Appcountratinguser`='$_POST[update_appcountratinguser]',`A_Applanguage`='$_POST[update_applanguage]',`A_Appdownload`='$_POST[update_appdownload]',`A_Appadid`='$update',`A_Apppadid`='$pushpid',`A_Appstatus`='1',`A_Applike`='$_POST[update_applike]',`A_Appjailbreak`='$_POST[update_appjailbreak]',`A_Applike`='$_POST[update_applike]',`A_Appdate`='$date' WHERE A_Apptrackid = '$url_trackid'";
			$query = $mysqli->query($sql);
			if($query){
				echo "<script>alert('更新成功')</script>";
				echo "<meta http-equiv=\"refresh\" content=\"1; url='admin_app_mangerment.php?page=1' />";
			}else{
				echo "<script>alert('更新失败')</script>";
				echo $mysqli->error;
				echo "<meta http-equiv=\"refresh\" content=\"1; url='admin_app_edit.php?".$url_trackid."' />";
			}
		}
		
		$sql_select_detail = "select * from appinfo where A_Apptrackid = '$url_trackid'";
		$query_select_detail = $mysqli->query($sql_select_detail);
		$result_select_detail = $query_select_detail->fetch_assoc();
		print_r($result_select_detail);

	

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<title>后台首页</title>
<style>
	body{ background:#e7e7e7; margin:0; padding:0;}
	#container{ width:980px; height:auto; position:relative; margin:0 auto;}
	#container #header{ width:980px; height:90px; position:relative; background:#000; line-height:90px;color:#fff; font-size:24px; text-align:center;}
	.left{width:250px; height:auto; position:relative; float:left;}
	.left table{width:248px; height:auto; position:relative; float:left; background:#fff;}
	.left table td{ height:20px; text-align:center;}
	.left table .selected{ background:#73abe3;}
	#content{ width:730px; height:100%; position:relative; float:left; margin:2px 0px;}
	#content .title{ width:720px; height:25px; position:relative; float:left; line-height:25px; font-size:18px; color:#fff; background:#000;}
	form{margin:20px auto; width:720px; height:auto; position:relative; float:left;}
	form .inpuarea{ width:650px; height:450px;}
</style>
</head>
<body>
	<div id="container">
		<div id="header">
			818苹果站后台管理
		</div>
		<div class="left">
			<table>
				<tr><td style="background:#000000; color:#fff;">站点管理</td></tr>
				<tr><td><a href="admin_site_mangerment.php">全局管理</a></td></tr>
				<tr><td>静态页面管理</td></tr>
				<tr><td><a href="admin_link_mangerment.php?page=1">友情链接管理</a></td></tr>
				<tr><td><a href="admin_member_mangerment.php?page=1">会员管理</a></td></tr>
				<tr><td><a href="admin_tags_mangerment.php?page=1">标签管理</a></td></tr>
				<tr><td><a href="admin_search_mangerment.php">搜索管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">文章管理</td></tr>
				<tr><td><a href="admin_category_mangerment.php">文章分类管理</a></td></tr>
				<tr><td><a href="admin_news_add.php">发布文章</a></td></tr>
				<tr><td><a href="admin_news_mangerment.php?page=1">文章管理</a></td></tr>
			</table>
			<table>
				<tr><td style="background:#000000; color:#fff;">应用管理</td></tr>
				<tr><td><a href="admin_appcate_mangerment.php">应用分类管理</a></td></tr>
				<tr><td><a href="admin_app_add.php">添加应用</a></td></tr>
				<tr><td class="selected"><a href="admin_app_mangerment.php?page=1">应用管理</a></td></tr>
                <tr><td><a href="admin_app_collection.php">应用采集</a></td></tr>
				<tr><td><a href="admin_app_collection_single.php">单应用采集</a></td></tr>
			</table>
		</div>
	<div id="content">
		<div class="title">发布APP</div>
		<form action="" method="post" enctype="multipart/form-data">
		<table>
			<tr>
				<td><label>APP标题</label></td><td><?php echo $result_select_detail['A_Apptrackid'];?><input class="input" type="text" name="update_apptrackname" size="50" value="<?php echo trim($result_select_detail['A_Apptrackname']);?>"/>(最大长度25个字)</td>
			</tr>
            <tr>
				<td><label>APP分类</label></td><td>
                <?php if($result_select_detail['A_Class'] == '0'){?>
                <input type="radio" name="update_class" value="0" checked="checked"/>IOS_APP<input type="radio" name="update_class" value="1" />越狱APP
                <?php }else{?>
                <input type="radio" name="update_class" value="0"/>IOS_APP<input type="radio" name="update_class" value="1" checked="checked"/>越狱APP
                <?php }?>
                </td>
			</tr>
            <tr>
				<td><label>APP类别</label></td><td>
                <?php 
					$categoryid = $result_select_detail['A_Categoryid'];
					$sql_select_categoryid = "select * from appcategoryinfo";
					$query_select_categoryid = $mysqli->query($sql_select_categoryid);
					while($result_select_categoryid = $query_select_categoryid->fetch_assoc()){
				?>
                <input type="radio" name="update_category" size="50" value="<?php echo $result_select_categoryid['AC_Categoryid'];?>" 
                <?php
                	if($result_select_categoryid['AC_Categoryid'] == $categoryid){
				?>
					checked="
                <?php 
                    echo "checked";
					}
				?>
                "/>
				<?php 
					$categoryname = $result_select_categoryid['AC_Categoryname'];
					echo $categoryname;
					$sql_select_categorykid = "select K_KID from keywordinfo where K_Name = '$categoryname'";
					$query_select_categorykid = $mysqli->query($sql_select_categorykid);
					$result_select_categorykid = $query_select_categorykid->fetch_assoc();
					echo "(".$result_select_categorykid['K_KID'].")";?><!--<input type="radio" name="app_class" value="1" />动作<input type="radio" name="app_class" value="0"/>策略<input type="radio" name="app_class" value="0"/>休闲<input type="radio" name="app_class" value="0"/>射击-->
                <?php
                	}					
				?></td>
			</tr>
			<tr>
				<td><label>APP关键词</label></td><td><input type="text"  size="50" name="update_keyword" value="<?php
					$appkid = $result_select_detail['A_Appkid'];
                	$appkid_explode = explode(',',$appkid);
					foreach($appkid_explode as $key=>$value){
						$value=$appkid_explode[$key];
						$sql_select_kid = "select K_Name from keywordinfo where K_KID = '$value'";
						$query_select_kid = $mysqli->query($sql_select_kid);
						while($result_select_kid = $query_select_kid->fetch_assoc()){
							echo $result_select_kid['K_Name'].',';
						}
					}
				?>" />(最大长度50个字)<input type="submit" name="check_keyword" value="关键词转换" /></td>
			</tr>
            <tr>
            	<td></td><td>
                <?php
                	if(isset($_POST['check_keyword'])){
						$keyword = $_POST['update_keyword'];
						$keyword_explode = explode(",",$keyword);
						$count = count($keyword_explode);
						for($i=0;$i<$count;$i++){
						$sql = "select * from keywordinfo where K_Name = '$keyword_explode[$i]'";
						$query = $mysqli->query($sql);
						$result = $query->fetch_assoc();
						if($result){
							$new = $result['K_KID'];
							echo $result['K_KID'].",";
            			}else{
							$sql = "INSERT INTO keywordinfo (K_KID, K_ID, K_Name) VALUES (NULL, NULL, '$keyword_explode[$i]')";
							$query = $mysqli->query($sql);
				
							$sql_select = "select K_KID from keywordinfo where K_Name = '$keyword_explode[$i]'";
							$query_select = $mysqli->query($sql_select);
							$result_select = $query_select->fetch_assoc();
							echo $result_select['K_KID'].",";
						}
					}	
				}		
			?></td><td></td>
                <!--<input class="input" type="text" name="app_keyword" value="" />(最大长度50个字)-->
			</tr>
			<tr>
				<td><label>APP价格</label></td>
                <td>
                    <input type="text" name="update_price"  size="50" value="<?php echo $result_select_detail['A_Appprice'];?>" />(精确到小数点后两位)</td>
			</tr>
            <tr>
            	<td><label>APP厂商ID</label></td>
                <td><input type="text" name="update_appsystem" size="50" value="<?php echo $result_select_detail['A_Appsystem'];?>" />
                </td>
            </tr>
            <tr>
            	<td><label>APP厂商名称</label></td>
                <td><input type="text" size="50" name="update_appartistname" value="<?php echo $result_select_detail['A_Appartistname'];?>" />
                </td>
            </tr>
            <tr>
            	<td><label>APP语言</label></td>
                <td><input type="text" size="50" name="update_applanguage" value="<?php echo $result_select_detail['A_Applanguage'];?>" />
            	</td>
            </tr>
            <tr>
            	<td><label>APP下载地址</label></td>
                <td><input type="text" size="50" name="update_appdownload" value="<?php echo $result_select_detail['A_Appdownload'];?>" />
            	</td>
            </tr>
			<tr>
				<td><label>APP越狱下载地址</label></td>
				<td><input type="text" size="50" name="update_appjailbreak" value="<?php echo $result_select_detail['A_Appjailbreak'];?>" /></td>
			</tr>
			<tr>
				<td><label>APP下载次数</label></td>
				<td><input type="text" size="50" name="update_appcount" value="<?php echo $result_select_detail['A_Appcount']?>" /></td>
			</tr>
            <tr>
            	<td><label>APP喜欢人数</label></td>
                <td><input type="text" size="50" name="update_applike" value="<?php echo $result_select_detail['A_Applike'];?>" />
            	</td>
            </tr>
            <tr>
            	<td><label>APP发布时间</label></td>
                <td><input type="text" size="50" name="update_appreleasedate" value="<?php echo $result_select_detail['A_Appreleasedate'];?>" />
            	</td>
            </tr>
           	<tr>
            	<td><label>APP图标(60*60)</label></td>
                <td><input type="text" size="50" name="update_appicon" value="<?php echo $result_select_detail['A_Appicon'];?>" />
            	</td>
            </tr>
            <tr>
            	<td><label>APP截图</label></td>
                <td><input size="50" name="update_appscreenshot" value="<?php echo $result_select_detail['A_Apppid'] ?>"/></td>
            </tr>
            <tr>
            	<td><label>APP大小</label></td>
                <td><input type="text" size="50" name="update_appfilesizebytes" value="<?php echo $result_select_detail['A_Appfilesize'];?>" />
            	</td>
            </tr>
            <tr>
            	<td><label>APP评分</label></td>
                <td><input type="text" size="50" name="update_apprating" value="<?php echo $result_select_detail['A_Apprating'];?>" />
            	</td>
            </tr>
            <tr>
            	<td><label>APP评分人数</label></td>
                <td><input type="text" size="50" name="update_appcountratinguser" value="<?php echo $result_select_detail['A_Appcountratinguser'];?>" />
            	</td>
            </tr>
            <tr>
            	<td><label>APP版本号</label></td>
                <td><input type="text" size="50" name="update_appversion" value="<?php echo $result_select_detail['A_Appversion'];?>" />
                </td>
            </tr>
            <tr>
				<td><label>幻灯推送位置</label></td><td><input type="checkbox" name="app_overhead" value="0" />无推广<input type="checkbox" name="app_overhead" value="1" />幻灯1<input type="checkbox" name="app_overhead" value="2" />幻灯2<input type="checkbox" name="app_overhead" value="3" />幻灯3<input type="checkbox" name="app_overhead" value="4" />幻灯4<input type="checkbox" name="app_overhead" value="5" />幻灯5<input type="checkbox" name="app_overhead" value="6" />幻灯6<input type="checkbox" name="app_overhead" value="7" />幻灯7<input type="checkbox" name="app_overhead" value="8" />幻灯8<input type="checkbox" name="app_overhead" value="9" />幻灯9<input type="checkbox" name="app_overhead" value="10" />幻灯10</td>
			</tr>
			<tr>
				<td><label>限免推送位置</label></td><td><input type="checkbox" name="app_overhead" value="13" />首页限免1<input type="checkbox" name="app_overhead" value="14" />首页限免2<input type="checkbox" name="app_overhead" value="15" />首页限免3<input type="checkbox" name="app_overhead" value="16" />首页限免4<input type="checkbox" name="app_overhead" value="17" />首页限免5<input type="checkbox" name="app_overhead" value="18" />首页限免</td>
			</tr>
			<tr>
				<td><label>通栏推送位置</label></td><td><input type="checkbox" name="app_overhead" value="31" />顶部banner<input type="checkbox" name="app_overhead" value="32" />通栏推广1<input type="checkbox" name="app_overhead" value="33" />通栏推广2</td>
			<tr>
				<td><label>右侧推送位</label></td><td><input type="checkbox" name="app_overhead" value="21" />右侧APP推广1<input type="checkbox" name="app_overhead" value="22" />右侧APP推广2</td>
			</tr>
			<tr>
				<td><label>列表推送位</label></td><td><input type="checkbox" name="app_overhead" value="23" />列表推广1<input type="checkbox" name="app_overhead" value="24" />列表推广2<input type="checkbox" name="app_overhead" value="25" />列表推广3<input type="checkbox" name="app_overhead" value="26" />列表推广4<input type="checkbox" name="app_overhead" value="27" />列表推广5<input type="checkbox" name="app_overhead" value="28" />列表推广6<input type="checkbox" name="app_overhead" value="29" />列表推广5<input type="checkbox" name="app_overhead" value="30" />列表推广8</td>
			</tr>
            <tr>
            	<td><label>配图</label></td><td><input type="file" name="uploaded" /><input type="text" name="uploaded_name" /></td>
            </tr>
			<tr>
				<td><label>APP描述</label></td><td><textarea class="inpuarea" name="update_desc"  />
                <?php
                	 echo $result_select_detail['A_Appdesc'];
				?>
                </textarea>(最大长度120个字)</td>
			</tr>
			<tr>
				<td><input type="submit" name="update_app" value="提交" /></td>
			</tr>
		</table>
		</form>
        </table>
	</div>
</div>
</body>
</html>
<?php
	}else{
		echo "<script>alert('你干嘛！登录先！')</script>";
		echo "<meta http-equiv=\"refresh\" content=\"1; url='admin_login.php' />";
	}

?>